package com.heima.article.controller.v1;

import com.heima.article.service.ArticleHomeService;
import com.heima.common.constants.article.ArticleConstants;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.article.dto.ArticleHomeDto;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;


/**
 * @author Ronaldo
 * @date 2021/8/13
 */
@RestController
@RequestMapping("/api/v1/article")
@Slf4j
public class ArticleHomeController {

    @Autowired
    private ArticleHomeService articleHomeService;

    @PostMapping("/load")
    public ResponseResult getArticle(@RequestBody ArticleHomeDto articleHomeDto){
        log.info("get article by articleHomeDto");

        if (Objects.isNull(articleHomeDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return articleHomeService.getArticle(articleHomeDto, ArticleConstants.LOAD_TYPE_LOAD_MORE);
    }

    @PostMapping("/loadmore")
    public ResponseResult getMoreArticle(@RequestBody ArticleHomeDto articleHomeDto){
        log.info("get more article by articleHomeDto");

        if (Objects.isNull(articleHomeDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return articleHomeService.getArticle(articleHomeDto, ArticleConstants.LOAD_TYPE_LOAD_MORE);
    }

    @PostMapping("/loadnew")
    public ResponseResult getNewArticle(@RequestBody ArticleHomeDto articleHomeDto){
        log.info("get new article by articleHomeDto");

        if (Objects.isNull(articleHomeDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return articleHomeService.getArticle(articleHomeDto, ArticleConstants.LOAD_TYPE_LOAD_NEW);
    }
}
